package com.woniuxy.orderassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.common.PageResult;
import com.woniuxy.common.Result;
import com.woniuxy.form.ModifyComplainForm;
import com.woniuxy.form.ModifyDrawbackForm;
import com.woniuxy.form.OrderComplainForm;
import com.woniuxy.form.OrderDrawbackForm;
import com.woniuxy.orderassembly.model.OrderComplain;
import com.woniuxy.orderassembly.model.OrderDrawback;
import com.woniuxy.orderassembly.dao.OrderDrawbackMapper;
import com.woniuxy.orderassembly.service.IOrderDrawbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chestnut
 * @since 2021-05-24
 */
@Service
@Slf4j
public class OrderDrawbackServiceImpl extends ServiceImpl<OrderDrawbackMapper, OrderDrawback> implements IOrderDrawbackService {

    @Resource
    private OrderDrawbackMapper orderDrawbackMapper;
    @Override
    public Result addOrderDrawback(OrderDrawbackForm orderDrawbackForm) {
        OrderDrawback orderDrawback = new OrderDrawback();
        log.info("生成订单退款表信息,{}",orderDrawbackForm);
        BeanUtils.copyProperties(orderDrawbackForm,orderDrawback);
        //生成订单投诉表时 默认为 1/待处理 2/已处理
        orderDrawback.setStatus(1);
        orderDrawback.setResult("嘀嘀嘀 默认未处理");
        int i = orderDrawbackMapper.insert(orderDrawback);
        return i>0?Result.success("success"):Result.success("fail");
    }

    @Override
    public Result modifyOrderDrawback(ModifyDrawbackForm modifyDrawbackForm) {
        UpdateWrapper<OrderDrawback> wrapper = new UpdateWrapper<>();
        log.info("修改订单投诉表信息,{}",modifyDrawbackForm);
        wrapper.set("reason",modifyDrawbackForm);
        wrapper.set("desc",modifyDrawbackForm.getDesc());
        wrapper.eq("id",modifyDrawbackForm.getId());
        int i = orderDrawbackMapper.update(null,wrapper);
        return i>0?Result.success("success"):Result.success("fail");
    }

    @Override
    public Result delOrderDrawback(Integer id) {
        log.info("根据id进行订单投诉表的删除,{}",id);
        int i = orderDrawbackMapper.deleteById(id);
        return i>0?Result.success("success"):Result.success("fail");
    }

    @Override
    public Result pageOrderDrawback(Integer pageIdx) {
        //查询条件
        QueryWrapper<OrderDrawback> wrapper = new QueryWrapper<>();
        //当前页码，每页条数
        Page<OrderDrawback> pageParam = new Page(pageIdx,pageIdx);
        IPage<OrderDrawback> pageResult = orderDrawbackMapper.selectPage(pageParam,wrapper);

        PageResult<OrderDrawback> myPageResult =new PageResult<>((int)pageResult.getTotal(),(int)pageResult.getCurrent(),(int)pageResult.getSize(),pageResult.getRecords());
        myPageResult.setSuccess(true);
        return myPageResult;
    }

    @Override
    public Result pageOrderDrawbackByStatus(Integer status, Integer pageIdx) {
        QueryWrapper<OrderDrawback> wrapper = new QueryWrapper<>();
        wrapper.eq("status",status);
        Page<OrderDrawback> pageParam = new Page(pageIdx,pageIdx);
        IPage<OrderDrawback> pageResult = orderDrawbackMapper.selectPage(pageParam,wrapper);
        PageResult<OrderDrawback> myPageResult =new PageResult<OrderDrawback>((int)pageResult.getTotal(),(int)pageResult.getCurrent(),(int)pageResult.getSize(),pageResult.getRecords());
        myPageResult.setSuccess(true);
        return myPageResult;
    }
}
